package com.ruoyi.system.controller;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.uuid.IdUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TAttachment;
import com.ruoyi.system.service.ITAttachmentService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 附件信息Controller
 * 
 * @author ruoyi
 * @date 2024-03-20
 */
@Controller
@RequestMapping("/system/attachment")
public class TAttachmentController extends BaseController
{
    private String prefix = "system/attachment";

    @Autowired
    private ITAttachmentService tAttachmentService;

    @RequiresPermissions("system:attachment:view")
    @GetMapping()
    public String attachment()
    {
        return prefix + "/attachment";
    }

    /**
     * 查询附件信息列表
     */
    @RequiresPermissions("system:attachment:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TAttachment tAttachment)
    {
        startPage();
        List<TAttachment> list = tAttachmentService.selectTAttachmentListByAttachId(tAttachment);
        return getDataTable(list);
    }

    @RequiresPermissions("system:attachment:list")
    @PostMapping("/houselist")
    @ResponseBody
    public TableDataInfo houselist(TAttachment tAttachment)
    {
        startPage();
        List<TAttachment> list = tAttachmentService.selectHouseTAttachmentList(tAttachment);
        return getDataTable(list);
    }

    @RequiresPermissions("system:attachment:list")
    @PostMapping("/landlist")
    @ResponseBody
    public TableDataInfo landlist(TAttachment tAttachment)
    {
        startPage();
        List<TAttachment> list = tAttachmentService.selectLandTAttachmentList(tAttachment);
        return getDataTable(list);
    }

    /**
     * 导出附件信息列表
     */
    @RequiresPermissions("system:attachment:export")
    @Log(title = "附件信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TAttachment tAttachment)
    {
        List<TAttachment> list = tAttachmentService.selectTAttachmentList(tAttachment);
        ExcelUtil<TAttachment> util = new ExcelUtil<TAttachment>(TAttachment.class);
        return util.exportExcel(list, "附件信息数据");
    }

    /**
     * 新增附件信息
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存附件信息
     */
    @RequiresPermissions("system:attachment:add")
    @Log(title = "附件信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(TAttachment tAttachment)
    {
        String id = tAttachment.getId();
        if (StringUtils.isEmpty(tAttachment.getId())){
            tAttachment.setId(IdUtils.randomUUID());
        }
        return toAjax(tAttachmentService.insertTAttachment(tAttachment));
    }

    /**
     * 修改附件信息
     */
    @RequiresPermissions("system:attachment:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TAttachment tAttachment = tAttachmentService.selectTAttachmentById(id);
        mmap.put("tAttachment", tAttachment);
        return prefix + "/edit";
    }

    /**
     * 修改保存附件信息
     */
    @RequiresPermissions("system:attachment:edit")
    @Log(title = "附件信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TAttachment tAttachment)
    {
        return toAjax(tAttachmentService.updateTAttachment(tAttachment));
    }


    @PostMapping("/edit/{id}")
    @ResponseBody
    public AjaxResult editDel(TAttachment tAttachment)
    {
        tAttachment.setDelFlag(1);
        int i = tAttachmentService.updateTAttachment(tAttachment);
        return toAjax(i);
    }

    /**
     * 删除附件信息
     */
    @RequiresPermissions("system:attachment:remove")
    @Log(title = "附件信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int i = tAttachmentService.deleteTAttachmentByIds(ids);
        return toAjax(i);
    }
}
